<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/27
 * Time: 18:33
 */

namespace App\Task;


use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Curl;
use EasySwoole\EasySwoole\Swoole\Task\AbstractAsyncTask;
use EasySwoole\MysqliPool\Mysql;

class MatchListsTask extends AbstractAsyncTask
{
    public function run($taskData, $taskId, $fromWorkerId, $flags = null)
    {
        $i = date('i',time());
        if( !is_int($i/20) ) {
            return ;
        }
        $params = Auth::getInstance($taskData)->params();
        $response = Curl::request('GET',UrlEnums::XXEURL,[
            'query' => $params
        ]);
        $content = $response->getBody();
        $content = json_decode($content, true);
        if( !isset($content['data']) || empty($content['data']) ){
            return;
        }
        $data = $content['data'];
        $db = Mysql::defer('mysql');
        foreach ($data as $k=>$v){
            $res = $db->where('game_id',$v['game_id'])->getOne('sc_match_lists');
            if( !$res ){
                $db->insert('sc_match_lists',[
                    'dimension' => (string)$v['dimension'],
                    'fight_name'=> $v['fight_name'],
                    'map_name'  => $v['map_name'],
                    'event_name'=> $v['event_name'],
                    'event_id'  => (int)$v['event_id'],
                    'game_id'   => (int)$v['game_id'],
                    'game_status'=> (string)$v['game_status'],
                    'game_status_type' => (string)$v['game_status_type'],
                    'match_stage'=> $v['match_stage'],
                    'match_stage_id' => $v['match_stage_id'],
                    'match'     => $v['match'],
                    'match_id'  => (int)$v['match_id'],
                    'match_name'=> $v['match_name'],
                    'match_info'=> $v['match_info'],
                    'stage_id'  => (int)$v['stage_id'],
                    'game_type' => $v['game_type'],
                    'player_a'  => json_encode($v['player_a']),
                    'player_b'  => json_encode($v['player_a']),
                    'team_a'    => json_encode($v['team_a']),
                    'team_b'    => json_encode($v['team_b']),
                    'score_a'   => $v['score_a'],
                    'score_b'   => $v['score_b'],
                    'is_live'   => (string)$v['is_live'],
                    'bo'        => (int)$v['bo'],
                    'hot_game'  => (string)$v['hot_game'],
                    'begin_time'=> $v['begin_time'],
                ]);
            }
        }
    }

    public function finish($result, $task_id)
    {
        // TODO: Implement finish() method.
    }
}